package vn.allmobile.ambnews.activities.main;

import java.util.List;

import vn.allmobile.ambnews.activities.category.CategoryActivity;
import vn.allmobile.ambnews.customview.LoadingDialog;
import vn.allmobile.ambnews.data.Category;
import vn.allmobile.ambnews.data.Paper;
import vn.allmobile.ambnews.network.LoadDataListener;
import vn.allmobile.ambnews.network.NetworkAPI;
import vn.allmobile.ambnews.utils.Contants;
import vn.allmobile.ambnews.utils.GlobalApplication;
import vn.allmobile.ambnews.utils.Utils;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import com.google.analytics.tracking.android.EasyTracker;
import com.google.analytics.tracking.android.Tracker;

public class MainItemClickListener implements OnClickListener,LoadDataListener {
	Context _context;
	int _paperIndex;
	LoadingDialog _dialog;
	public Handler mHandle = new Handler()
	{
	  public void handleMessage(Message paramAnonymousMessage)
	  {
	    switch (paramAnonymousMessage.what)
	    {
	    	case 0:
	    		//success
	    		//pbarDialog.dismiss();
	    		Context _context = GlobalApplication._();
	    		_dialog.hide();
	    		Intent startCat = new Intent(_context,CategoryActivity.class);
	    		startCat.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				_context.startActivity(startCat);
	    		break;
	    	case 1:
	    		// OnCanCel
	    		_dialog.hide();
	    		//NetworkAPI._()._loaderThread.loadMsg = -1;
	    		break;
	    }
	  }
	};
	public MainItemClickListener(int paperIndex) {
		
		_paperIndex = paperIndex;		
	}

	@Override
	public void onClick(View arg0) {
		Context _context = GlobalApplication._();
		
		
	      
		// TODO Auto-generated method stub
		NetworkAPI._currentPaperIndex = _paperIndex;
		
		Paper pp = NetworkAPI._papers.get(_paperIndex);
		
		NetworkAPI._currentPaperId = pp.getId();
		
		
		//TRACKING PAPER
		EasyTracker.getInstance().setContext(GlobalApplication._());			
	    Tracker tracker = EasyTracker.getTracker();
		tracker.sendEvent(Contants.TRACKER_PAPER_BUTTONS,NetworkAPI._currentPaperId + "-" + Contants.TRACKER_ACTIONS_CLICKED,pp.getTitle() , Utils.currentMillis());

		_dialog = new LoadingDialog(arg0.getContext());
		//_dialog  = new LoadingDialog(arg0.getContext(), mHandle,1);
		_dialog.show();
		List<Category> categories = pp.getCategories();
		
		
		if(categories.size() == 0)
		{
			
			NetworkAPI._()._loaderThread.setloadDataListener(this);			
			//this._mRequest = NetworkAPI._()._loaderThread;
			NetworkAPI.onSetRequest(Contants.MSG_LOAD_CAT);
	
		}
		else
		{			
			this.mHandle.sendEmptyMessage(0);
		}
					
	}
	
	@Override
	public void onPaperloadSuccess() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onCategoryloadSuccess() {
		// TODO Auto-generated method stub
		this.mHandle.sendEmptyMessage(0);
	}

	@Override
	public void onListloadSuccess() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onListloadFail() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onUserloadSuccess() {
		// TODO Auto-generated method stub
		
	}

}
